package com.example.wzdjcrdpassword.controller;

import com.example.wzdjcrdpassword.entity.SystemEntity;
import com.example.wzdjcrdpassword.service.SystemService;
import com.example.wzdjcrdpassword.util.RequestContextUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/api/system")
@RequiredArgsConstructor
@Slf4j
public class SystemController {
    
    private final SystemService systemService;
    
    /**
     * 获取所有有效的系统
     * @return 系统列表
     */
    @GetMapping("/all")
    public ResponseEntity<Map<String, Object>> getAllSystems() {
        List<SystemEntity> systems = systemService.getAllActiveSystems();
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("data", systems);
        response.put("total", systems.size());
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 根据系统ID获取系统信息
     * @param systemId 系统ID
     * @return 系统信息
     */
    @GetMapping("/{systemId}")
    public ResponseEntity<Map<String, Object>> getSystemById(@PathVariable String systemId) {
        Optional<SystemEntity> systemOpt = systemService.getSystemById(systemId);
        
        Map<String, Object> response = new HashMap<>();
        if (systemOpt.isPresent()) {
            response.put("success", true);
            response.put("data", systemOpt.get());
        } else {
            response.put("success", false);
            response.put("message", "系统不存在或已被删除");
        }
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 根据系统类型获取系统列表
     * @param systemType 系统类型
     * @return 系统列表
     */
    @GetMapping("/type/{systemType}")
    public ResponseEntity<Map<String, Object>> getSystemsByType(@PathVariable String systemType) {
        List<SystemEntity> systems = systemService.getSystemsByType(systemType);
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("data", systems);
        response.put("total", systems.size());
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 根据系统名称搜索系统
     * @param systemName 系统名称
     * @return 系统列表
     */
    @GetMapping("/search")
    public ResponseEntity<Map<String, Object>> searchSystemsByName(@RequestParam String systemName) {
        List<SystemEntity> systems = systemService.searchSystemsByName(systemName);
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("data", systems);
        response.put("total", systems.size());
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 创建新系统
     * @param systemEntity 系统信息
     * @return 创建结果
     */
    @PostMapping("/create")
    public ResponseEntity<Map<String, Object>> createSystem(@RequestBody SystemEntity systemEntity) {
        // 记录调用来源信息
        String appId = RequestContextUtil.getCurrentAppId();
        String systemName = RequestContextUtil.getCurrentSystemName();
        log.info("创建系统请求 - appId: {}, 系统名称: {}, 新系统ID: {}", 
                appId, systemName, systemEntity.getSystemId());
        
        SystemEntity createdSystem = systemService.createSystem(systemEntity);
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "系统创建成功");
        response.put("data", createdSystem);
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 更新系统信息
     * @param systemEntity 系统信息
     * @return 更新结果
     */
    @PutMapping("/update")
    public ResponseEntity<Map<String, Object>> updateSystem(@RequestBody SystemEntity systemEntity) {
        // 记录调用来源信息
        String appId = RequestContextUtil.getCurrentAppId();
        String systemName = RequestContextUtil.getCurrentSystemName();
        log.info("更新系统请求 - appId: {}, 系统名称: {}, 目标系统ID: {}", 
                appId, systemName, systemEntity.getSystemId());
        
        SystemEntity updatedSystem = systemService.updateSystem(systemEntity);
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "系统更新成功");
        response.put("data", updatedSystem);
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 删除系统（逻辑删除）
     * @param systemId 系统ID
     * @return 删除结果
     */
    @DeleteMapping("/{systemId}")
    public ResponseEntity<Map<String, Object>> deleteSystem(@PathVariable String systemId) {
        // 记录调用来源信息
        String appId = RequestContextUtil.getCurrentAppId();
        String systemName = RequestContextUtil.getCurrentSystemName();
        log.info("删除系统请求 - appId: {}, 系统名称: {}, 目标系统ID: {}", 
                appId, systemName, systemId);
        
        boolean isDeleted = systemService.deleteSystem(systemId);
        
        Map<String, Object> response = new HashMap<>();
        if (isDeleted) {
            response.put("success", true);
            response.put("message", "系统删除成功");
        } else {
            response.put("success", false);
            response.put("message", "系统不存在或已被删除");
        }
        
        return ResponseEntity.ok(response);
    }
}